<?php

define("FACEBOOK_EXTERNAL_SYSTEM_ID",1);


function getPatronInformation( $authToken,$externalSystemID,$forceCacheRefresh)
{
	// Setup the RemoteFunction parameters
	$ap_param = array(
		'authToken' => $authToken,
		'externalSystemID' => $externalSystemID,
		'forceCacheRefresh' => $forceCacheRefresh
	);
	return getSOAPResponse( "getPatronInformation", $ap_param );
}

function getTop5Loans( $authToken, $externalSystemID, $courseCode)
{
	// Setup the RemoteFunction parameters
	$ap_param = array(
		'authToken' => $authToken,
		'externalSystemID' => $externalSystemID,
		'courseCode' => $courseCode
	);
	return getSOAPResponse( "getTop5Loans", $ap_param );
}





function renewItem( $itemID, $itemSequence, $borID )
{
	// Setup the RemoteFunction parameters
	$ap_param = array( 'itemID' => $itemID, 'itemSequence' => $itemSequence, 'borID' => $borID );
	return getSOAPResponse( "renewItem", $ap_param );
}

function renewAllItems( )
{
	// Setup the RemoteFunction parameters
	$ap_param = array( );
	return getSOAPResponse( "renewAllItems", $ap_param );
}

function getSOAPResponse( $soapFunctionName, $ap_param )
{
	try
	{
		global $user_id;
		$loginDetails = array('login' => "facebookuser", 'password' => "B00kOfFace");
		$soapClient = new SoapClient("http://soap.webapp4.uea.ac.uk:8080/services/WolfieWebService?wsdl",$loginDetails);

		$loans = $soapClient->__call($soapFunctionName, array($ap_param));
		return $loans->return;
	}
	catch (SoapFault $fault)
	{
		#echo "<pre>";
		#print_r($fault);
		#echo "</pre>";
		return "<p>There was an error connecting to the UEA web service. Please try again later, or report the error to <a href=\"mailto:web@uea.ac.uk\">web@uea.ac.uk</a>.</p>";
	}
}


function performXSLTTransform($sXml, $xsltFileName)
{
	# LOAD XML FILE
	$XML = new DOMDocument();
	$XML->loadXML( $sXml );

	# START XSLT
	$xslt = new XSLTProcessor();
	$XSL = new DOMDocument();
	$XSL->load( $xsltFileName, LIBXML_NOCDATA);

	$xslt->importStylesheet( $XSL );

	#PRINT
	return $xslt->transformToXML( $XML );
}



function debug($v)
{
	echo "<pre>";
	print_r($v);
	echo "</pre>";

}


function formatPatronInformation()
{
	$responseBody = null;

	try
	{
		$responseBody = getPatronInformation( "true" );
	}
	catch (Exception $e)
	{
		print_r($e);
	}

	// quick check for errors
	if( strpos ( $responseBody, "Error retrieving Patron System Key" ) )
	{
		echo( "<p>There was an error retrieving your library borrower record. Please contact technical support to investigate this.</p>" );
		return;
	}

	$finalBorrowingResult = $finalHoldingResult = $finalFinesResult = "";

	// perform the XSLT transformations
	try
	{
		$finalBorrowingResult = performXSLTTransform($responseBody, 'xsl/borrowing.xsl');
		$finalHoldingResult   = performXSLTTransform($responseBody, 'xsl/holdings.xsl');
		$finalFinesResult     = performXSLTTransform($responseBody, 'xsl/fines.xsl');
	}
	catch (Exception $xste)
	{
		//xste.printStackTrace(System.err);
		echo("XSTE exception = " + xste.getMessage());
	}

	if ($finalBorrowingResult == null || trim($finalBorrowingResult) == "")
	{
		$finalBorrowingHTML = "<h4>You have no borrowed books at the moment.</h4>";
	}
	else
	{
		$finalBorrowingHTML =
			"<h2>Current Loans</h2>".
			"<table class=\"libraryRecords\" border=\"0\" cellspacing=\"0\" cellpadding=\"1\" summary=\"This table contains a summary of your current library loans.\">" .
			"   <tr>" .
			"       <th scope=\"col\" width=\"50%\">Title</th>" .
			"       <th scope=\"col\">Due Date</th>" .
			"       <th scope=\"col\">Action</th>" .
			"   </tr>" .
			$finalBorrowingResult .
			"</table>";
	}

	if ($finalFinesResult == null || $finalFinesResult == "" )
	{
		$finalFinesHTML = "";
	}
	else
	{
		$finalFinesHTML =
			"<div id=\"libaryFines\">" .
			"<h2>Fines</h2>".
			$finalFinesResult.
			"</div>";
	}

	if ($finalHoldingResult == null || $finalHoldingResult == "")
	{
		$finalHoldingHTML = "<h2>You do not have any library reservations at the moment.</h2>";
	}
	else
	{
		$finalHoldingHTML =
			"<h2>Current Reservations</h2>".
			"<table class=\"libraryRecords\" border=\"0\" cellspacing=\"0\" cellpadding=\"1\" summary=\"This table contains a summary of your current library reservations.\">" .
			"   <tr>".
			"       <th scope=\"col\" width=\"50%\">Title</th>".
			"       <th scope=\"col\">Author</th>" .
			"       <th scope=\"col\">Request End Date</th>" .
			"   </tr>".
			$finalHoldingResult.
			"</table>";
	}
	$result = $finalBorrowingHTML . $finalFinesHTML . $finalHoldingHTML;
	// cache the HTML
	return $result;

}

?>